﻿using UnityEngine.Assertions;

namespace KuiHuaBaoDian {

    public interface IStorage {

        string AccountId { get; set; }

        string RoleId { get; set; }

        float GetFloat(string key, float @default = 0);

        float GetFloatWithAccount(string key, float @default = 0);

        float GetFloatWithRole(string key, float @default = 0);

        int GetInt(string key, int @default = 0);

        int GetIntWithAccount(string key, int @default = 0);

        int GetIntWithRole(string key, int @default = 0);

        string GetString(string key, string @default = "");

        string GetStringWithAccount(string key, string @default = "");

        string GetStringWithRole(string key, string @default = "");

        void SetFloat(string key, float value);

        void SetFloatWithAccount(string key, float value);

        void SetFloatWithRole(string key, float value);

        void SetInt(string key, int value);

        void SetIntWithAccount(string key, int value);

        void SetIntWithRole(string key, int value);

        void SetString(string key, string value);

        void SetStringWithAccount(string key, string value);

        void SetStringWithRole(string key, string value);
    }

    internal sealed class KHBD_Storage : IStorage {

        public string RoleId { get; set; }

        public string AccountId { get; set; }

        //
        public void SetInt(string key, int value) {
            Assert.IsFalse(string.IsNullOrWhiteSpace(key));

            UnityEngine.PlayerPrefs.SetInt(key, value);
        }

        public void SetFloat(string key, float value) {
            Assert.IsFalse(string.IsNullOrWhiteSpace(key));

            UnityEngine.PlayerPrefs.SetFloat(key, value);
        }

        public void SetString(string key, string value) {
            Assert.IsFalse(string.IsNullOrWhiteSpace(key));

            UnityEngine.PlayerPrefs.SetString(key, value);
        }

        public int GetInt(string key, int @default = 0) {
            Assert.IsFalse(string.IsNullOrWhiteSpace(key));

            if (!UnityEngine.PlayerPrefs.HasKey(key)) {
                UnityEngine.PlayerPrefs.SetInt(key, @default);
                return @default;
            }
            return UnityEngine.PlayerPrefs.GetInt(key);
        }

        public float GetFloat(string key, float @default = 0f) {
            Assert.IsFalse(string.IsNullOrWhiteSpace(key));

            if (!UnityEngine.PlayerPrefs.HasKey(key)) {
                UnityEngine.PlayerPrefs.SetFloat(key, @default);
                return @default;
            }
            return UnityEngine.PlayerPrefs.GetFloat(key);
        }

        public string GetString(string key, string @default = "") {
            Assert.IsFalse(string.IsNullOrWhiteSpace(key));

            if (!UnityEngine.PlayerPrefs.HasKey(key)) {
                UnityEngine.PlayerPrefs.SetString(key, @default);
                return @default;
            }
            return UnityEngine.PlayerPrefs.GetString(key);
        }

        //RoleId
        public void SetIntWithRole(string key, int value) {
            Assert.IsFalse(string.IsNullOrWhiteSpace(RoleId));

            key = $"{key}_@{RoleId}";
            SetInt(key, value);
        }

        public void SetFloatWithRole(string key, float value) {
            Assert.IsFalse(string.IsNullOrWhiteSpace(RoleId));

            key = $"{key}_@{RoleId}";
            SetFloat(key, value);
        }

        public void SetStringWithRole(string key, string value) {
            Assert.IsFalse(string.IsNullOrWhiteSpace(RoleId));

            key = $"{key}_@{RoleId}";
            SetString(key, value);
        }

        public int GetIntWithRole(string key, int @default = 0) {
            Assert.IsFalse(string.IsNullOrWhiteSpace(RoleId));

            key = $"{key}_@{RoleId}";
            return GetInt(key, @default);
        }

        public float GetFloatWithRole(string key, float @default = 0f) {
            Assert.IsFalse(string.IsNullOrWhiteSpace(RoleId));

            key = $"{key}_@{RoleId}";
            return GetFloat(key, @default);
        }

        public string GetStringWithRole(string key, string @default = "") {
            Assert.IsFalse(string.IsNullOrWhiteSpace(RoleId));

            key = $"{key}_@{RoleId}";
            return GetString(key, @default);
        }

        //AccountId
        public void SetIntWithAccount(string key, int value) {
            Assert.IsFalse(string.IsNullOrWhiteSpace(AccountId));

            key = $"{key}_#{AccountId}";
            SetInt(key, value);
        }

        public void SetFloatWithAccount(string key, float value) {
            Assert.IsFalse(string.IsNullOrWhiteSpace(AccountId));

            key = $"{key}_#{AccountId}";
            SetFloat(key, value);
        }

        public void SetStringWithAccount(string key, string value) {
            Assert.IsFalse(string.IsNullOrWhiteSpace(AccountId));

            key = $"{key}_#{AccountId}";
            SetString(key, value);
        }

        public int GetIntWithAccount(string key, int @default = 0) {
            Assert.IsFalse(string.IsNullOrWhiteSpace(AccountId));

            key = $"{key}_#{AccountId}";
            return GetInt(key, @default);
        }

        public float GetFloatWithAccount(string key, float @default = 0f) {
            Assert.IsFalse(string.IsNullOrWhiteSpace(AccountId));

            key = $"{key}_#{AccountId}";
            return GetFloat(key, @default);
        }

        public string GetStringWithAccount(string key, string @default = "") {
            Assert.IsFalse(string.IsNullOrWhiteSpace(AccountId));

            key = $"{key}_#{AccountId}";
            return GetString(key, @default);
        }
    }
}
